package com.nuorui.module.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuorui.common.constant.AdminSwaggerTagConst;
import com.nuorui.common.core.domain.ResponseDTO;
import com.nuorui.module.system.domain.form.UserForm;
import com.nuorui.module.system.domain.query.UserQueryForm;
import com.nuorui.module.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户管理
 *
 * @author: Fred
 * @email 453086@qq.com
 * @create: 2021-10-22 10:42
 */
@RestController
@RequiredArgsConstructor
@Api(tags = AdminSwaggerTagConst.System.SYSTEM_USER)
public class UserController {

    private final UserService userService;

    @GetMapping("/users/page")
    @ApiOperation(value = "分页", notes = "分页")
    public ResponseDTO page(Page page, UserQueryForm queryForm) {
        return userService.getUsersPage(page, queryForm);
    }

    @GetMapping("/users/department/{departmentId}/users")
    @ApiOperation(value = "根据部门ID获取用户列表", notes = "根据部门ID获取用户列表")
    public ResponseDTO getUsersByDepartmentId(@PathVariable Long departmentId) {
        return userService.getUsersByDepartmentId(departmentId);
    }

    @PostMapping("/users")
    @ApiOperation(value = "添加用户", notes = "添加用户")
    public ResponseDTO save(@Valid @RequestBody UserForm userForm) {
        return userService.saveUser(userForm);
    }

    @PutMapping("/users/{userId}")
    @ApiOperation(value = "更新用户", notes = "更新用户")
    public ResponseDTO update(@PathVariable Long userId, @Valid @RequestBody UserForm userForm) {
        return userService.updateUser(userId, userForm);
    }

    @PatchMapping("/users/{userId}/disabled")
    @ApiOperation(value = "更新禁用/启用状态", notes = "更新禁用/启用状态")
    public ResponseDTO updateDisable(@PathVariable Long userId) {
        return userService.updateDisable(userId);
    }

    @PatchMapping("/users/{userId}/password")
    @ApiOperation(value = "重置密码", notes = "重置密码")
    public ResponseDTO resetPassword(@PathVariable Long userId) {
        return userService.resetPassword(userId);
    }

    @DeleteMapping("/users")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public ResponseDTO batchDelete(@RequestBody List<Long> userIdList) {
        return userService.batchDelete(userIdList);
    }
}
